#!/usr/local/bin/php
<?php

define('IRC_ADDR', '206.53.56.35');
define('IRC_PORT', '6667');
define('MAXLINE', '16384');
define('NICK', 'IRC-LB-1');
define('ZONE', 'ircb.wyldryde.org');
define('FACTOR', '50');
define('DEBUG',1);

$arrServers = array(
	'Intrepid.WyldRyde.org' => '209.66.100.34',
	'Defiant.WyldRyde.org' => '63.243.153.249',
	'Rubicon.WyldRyde.org' => '206.53.63.3',
	'Odyssey.WyldRyde.org' => '64.85.162.192',
	'Ticonderoga.WyldRyde.org' => '206.53.56.35',
	'Sovereign.WyldRyde.org' => '206.53.49.4',
	'Apollo.WyldRyde.org' => '195.210.38.79',
	'Viper.WyldRyde.org' => '66.252.25.253',
	'Caprica.WyldRyde.org' => '203.33.71.117',
	'Cylon.WyldRyde.org' => '89.149.194.181',
	'Tranquility.WyldRyde.org' => '84.234.138.106',
	'Equilibrium.WyldRyde.org' => '82.192.74.54',
	'Raider.WyldRyde.org' => '72.20.49.132',
	'Athena.WyldRyde.org' => '193.23.141.248',
	'Frack.WyldRyde.org' => '195.210.38.112',
	'Resurrection.WyldRyde.org' => '216.86.156.48',	
);

set_time_limit(0);
ob_implicit_flush();

$irc = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$test = socket_connect($irc, IRC_ADDR, IRC_PORT);

$connected = 0;
$gomap = 0;

if (!$test) { echo "Count not Connect\n"; die; } else { echo "connected...\n"; }
$h = 0;
while(true) {
	++$h;
	$tabSocket[0] = $irc;
	$pret = socket_select($tabSocket, $null = null, $null = null, null);

	if (in_array($irc, $tabSocket)) {
		if (DEBUG > 1) { echo "DEBUG2: doing read\n"; }
		$buff = trim(socket_read($irc, MAXLINE));
		if (!$buff) {
			if (DEBUG > 1) { echo "DEBUG2: connect lost, reconnecting...\n"; }
			socket_shutdown($irc);
			socket_close($irc);
			$test = socket_connect($irc, IRC_ADDR, IRC_PORT);
			$connected = 0;
			if (!$test) { echo "reconnect failed\n"; break; } else { echo "reconnected...\n"; }
		} else {
			if (DEBUG > 2) { echo "DEBUG3: -> '".$buff."'\n"; }
			$tabout = explode("\n", $buff);
			foreach($tabout as $tbuff) {
				if (DEBUG > 0) { echo "DEBUG1: -> ".$tbuff."\n"; }
				$arrLine = explode(" ",$tbuff);
				$arrParams = explode(":",$tbuff);
				if ($arrLine[1]." ".$arrLine[2] == "NOTICE AUTH" && $connected == 0) {
					socket_write($irc,"USER IRC_LB IRC_LB IRC_LB :IRC Load Balancer by www.ThunderIT.com\n");
					socket_write($irc,"NICK ".NICK."\n");
					$connected = 1;
					continue;
				}
				if ($arrLine[0] == "PING") {
					socket_write($irc,"PONG :".$arrParams[1]."\n");
					socket_write($irc,"MAP\n");
					continue;
				}
				if ($arrLine[1] == "266") {
					socket_write($irc,"MAP\n");
					socket_write($irc,"JOIN #irc-lb\n");
					continue;
				}
				if ($arrLine[1] == "PRIVMSG" && $arrParams[2] == "!domap") {
					socket_write($irc,"NOTICE ".substr($arrLine[0],1,strpos($arrLine[0],'!') - 1)." :doing map...\n");
					socket_write($irc,"MAP\n");
					continue;
				}
				if ($arrLine[1] == "PRIVMSG" && $arrParams[2] == ''."VERSION".'') {
					socket_write($irc,"NOTICE ".substr($arrLine[0],1,strpos($arrLine[0],'!') - 1)." :".''."VERSION IRC-LB by www.ThunderIT.com".''."\n");
					continue;
				}
				if ($arrLine[1] == "006") {
					if ($gomap == 0) {
						//start of /map
						$arrMap = array();
					}
					$gomap = 1;
					//do map processing
					preg_match('/\|?\`?\-?(.+?)\s+\((\d+)\)/',$arrParams[2],$arrMatches);
					//echo (array_key_exists($arrMatches[1],$arrServers) ? "true" : "false");
					if ($arrMatches[2] > 0 && array_key_exists($arrMatches[1],$arrServers) === true) {
						$arrMap[$arrMatches[1]] = $arrMatches[2];
					}
					continue;
				}
				if ($arrLine[1] == "007") {
					$gomap = 0;
					print_r($arrMap);
					$fd = fopen("/usr/local/etc/dns_balance/wr_ircb.txt","w");
					foreach ($arrMap as $server => $users) {
						fwrite($fd,"default,".ZONE.",".$arrServers[$server].",".($users * FACTOR).",".$server."\n");
					}
					fclose($fd);
					$a = exec('cat /usr/local/etc/dns_balance/wr_ircb.txt | /usr/local/sbin/dns_makedb > /usr/local/etc/dns_balance/addr');
					continue;
				}
			}
		}
	}

}
socket_shutdown($irc);
socket_close($irc);


?>